import { _decorator, Component, Node, SpringJoint2D, Sprite, UITransform,Animation, AnimationClip, animation, SpriteFrame } from 'cc';
import EventManager from '../../Runtime/EventManger';
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_ENUM, PARAMS_NAME_ENUM, SHAKE_TYPE_ENUM } from '../../Enums';
import DataManager from '../../Runtime/DataManager';
import { EnemyManager } from '../../Base/EnemyManager';
import { IEntity } from '../../levels';
import { EntityManager } from '../../Base/EntityManager';
import { BurstStateMachine } from './BurstStateMachine';
import { TILE_HEIGHT, TILE_WIDTH } from '../Tile/TileManager';
const { ccclass, property } = _decorator;



@ccclass('BurstManager')
export class BurstManager extends EntityManager {
  // 初始化
  async init(params:IEntity) {
    // 3. 初始化状态机
    this.fsm = this.addComponent(BurstStateMachine)
    await this.fsm.init()
    super.init(params)  // 调用父类初始化
    const transform = this.getComponent(UITransform)
    transform.setContentSize(TILE_WIDTH , TILE_HEIGHT)

    EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END,this.onBurst,this)
  }

  onDestroy(){
    super.onDestroy()
    EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END,this.onBurst)
  }

  update() {
    // 设置节点位置（包含偏移计算）
    this.node.setPosition(
      this.x * TILE_WIDTH ,
      -this.y * TILE_HEIGHT
    )
  }

  onBurst(){
    if(this.state===ENTITY_STATE_ENUM.DEATH||!DataManager.Instance.player) return
    const {x:playerX,y:playerY,state:playerState}=DataManager.Instance.player

    if(this.x===playerX&&this.y===playerY&&this.state===ENTITY_STATE_ENUM.IDLE){
      this.state=ENTITY_STATE_ENUM.ATTACK
    }else if(this.state===ENTITY_STATE_ENUM.ATTACK){
      this.state=ENTITY_STATE_ENUM.DEATH
      EventManager.Instance.emit(EVENT_ENUM.SCREEN_SHAKE,SHAKE_TYPE_ENUM.BOTTOM)
      if(this.x===playerX&&this.y===playerY){
        EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER,ENTITY_STATE_ENUM.AIRDEATH)
      }
    }
  }

}
